package com.ymttest.database.sqlwapper;

import java.util.List;

import org.apache.commons.lang.math.RandomUtils;

import com.ymt.database.SqlManager;
import com.ymttest.database.model.YmtMobilePhoneAccount;
import com.ymttest.utils.EnvSetup;

/**
 * 查询绑定手机的新用户
 * 
 * @author wanghuiyong
 * @date 2015-06-12
 */
public class YmtMobilePhoneAccountWapper {
	SqlManager manage;

	public YmtMobilePhoneAccountWapper() {
		manage = new SqlManager("ymt" + EnvSetup.getEnv());

	}

	/**
	 * 获取一个新用户Id
	 * 
	 * @return
	 */
	public int getNewUserId() {
		List<YmtMobilePhoneAccount> a = manage.session.selectList("com.ymttest.database.mapping.YmtMobilePhoneAccountMapper.selectNewUser");
		int num = RandomUtils.nextInt(2);
		int UserId = a.get(num).getIuserid();
		return UserId;
	}

	/**
	 * 根据UserId获取用户名
	 * 
	 * @param UserId
	 * @return
	 */
	public String getAccountName(Integer UserId) {
		List<YmtMobilePhoneAccount> a = manage.session.selectList("com.ymttest.database.mapping.YmtMobilePhoneAccountMapper.selectAccountNameByUserId", UserId);
		String AccountName = a.get(0).getSaccountname();
		return AccountName;
	}

	/**
	 * 根据UserId获取AccountInfo
	 * 
	 * @param UserId
	 * @return YmtMobilePhoneAccount
	 */
	public YmtMobilePhoneAccount getAccountInfo(Integer UserId) {
		List<YmtMobilePhoneAccount> a = manage.session.selectList("com.ymttest.database.mapping.YmtMobilePhoneAccountMapper.selectAccountNameByUserId", UserId);
		if (a.size() != 0) {
			YmtMobilePhoneAccount AccountInfo = a.get(0);
			return AccountInfo;
		} else {
			return null;
		}
	}

	/**
	 * 随机获取一个绑定过手机号的用户
	 * 
	 * @return
	 */
	public YmtMobilePhoneAccount getAccountInfo() {
		List<YmtMobilePhoneAccount> a = manage.session.selectList("com.ymttest.database.mapping.YmtMobilePhoneAccountMapper.selectAccountByRandom");
		YmtMobilePhoneAccount AccountInfo = a.get(0);
		return AccountInfo;
	}

	/**
	 * 根据手机号获取用户
	 * 
	 * @param phone
	 * @return
	 */
	public YmtMobilePhoneAccount getAccountInfoByPhone(String phone) {
		List<YmtMobilePhoneAccount> a = manage.session.selectList("com.ymttest.database.mapping.YmtMobilePhoneAccountMapper.getAccountInfoByPhone", phone);
		if (a.size() == 1) {
			return a.get(0);
		}
		return null;
	}

	public void deleteByUid(Integer uid) {
		manage.session.delete("com.ymttest.database.mapping.YmtMobilePhoneAccountMapper.deleteByPrimaryKey", uid);
		manage.commit();
	}

	public static void main(String[] args) {
		YmtMobilePhoneAccountWapper test = new YmtMobilePhoneAccountWapper();
		Integer UserId = test.getNewUserId();
		test.getAccountName(UserId);
	}
}
